home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP08.ZIP / CHAP08 / PATRON / PATRON.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  11KB  |  467 lines

  1. /*
  2.  * PATRON.CPP
  3.  * Modifications for Chapter 8: None
  4.  *
  5.  * WinMain which is all we need for the basic application.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #define INITGUIDS
  18. #include "patron.h"
  19.  
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev
  32.     , LPSTR pszCmdLine, int nCmdShow)
  33.     {
  34.     LPCPatronFrame  pFR;
  35.     FRAMEINIT       fi;
  36.     WPARAM          wRet;
  37.  
  38.    #ifndef WIN32
  39.     SetMessageQueue(96);
  40.    #endif
  41.  
  42.     //Attempt to allocate and initialize the application
  43.     pFR=new CPatronFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  44.  
  45.     fi.idsMin=IDS_FRAMEMIN;
  46.     fi.idsMax=IDS_FRAMEMAX;
  47.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  48.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  49.     fi.idStatMenuMin=ID_MENUFILE;
  50.     fi.idStatMenuMax=ID_MENUHELP;
  51.     fi.iPosWindowMenu=WINDOW_MENU;
  52.     fi.cMenus=CMENUS;
  53.  
  54.     //If we can initialize pFR, start chugging messages
  55.     if (pFR->FInit(&fi))
  56.         wRet=pFR->MessageLoop();
  57.  
  58.     delete pFR;
  59.     return wRet;
  60.     }
  61.  
  62.  
  63.  
  64.  
  65. /*
  66.  * CPatronFrame::CPatronFrame
  67.  * CPatronFrame::~CPatronFrame
  68.  *
  69.  * Constructor Parameters:
  70.  *  hInst           HINSTANCE from WinMain
  71.  *  hInstPrev       HINSTANCE from WinMain
  72.  *  pszCmdLine      LPSTR from WinMain
  73.  *  nCmdShow        int from WInMain
  74.  */
  75.  
  76. CPatronFrame::CPatronFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  77.     , LPSTR pszCmdLine, int nCmdShow)
  78.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  79.     {
  80.     m_fInitialized=FALSE;
  81.     return;
  82.     }
  83.  
  84.  
  85. CPatronFrame::~CPatronFrame(void)
  86.     {
  87.     OleFlushClipboard();
  88.  
  89.     if (m_fInitialized)
  90.         OleUninitialize();
  91.     return;
  92.     }
  93.  
  94.  
  95.  
  96.  
  97. /*
  98.  * CPatronFrame::FInit
  99.  *
  100.  * Purpose:
  101.  *  Call OleInitialize then calling down into the base class
  102.  *  initialization.
  103.  *
  104.  * Parameters:
  105.  *  pFI             LPFRAMEINIT containing initialization parameters.
  106.  *
  107.  * Return Value:
  108.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  109.  */
  110.  
  111. BOOL CPatronFrame::FInit(LPFRAMEINIT pFI)
  112.     {
  113.     DWORD       dwVer;
  114.  
  115.     dwVer=OleBuildVersion();
  116.  
  117.     if (rmm!=HIWORD(dwVer))
  118.         return FALSE;
  119.  
  120.     if (FAILED(OleInitialize(NULL)))
  121.         return FALSE;
  122.  
  123.     m_fInitialized=TRUE;
  124.  
  125.     return CFrame::FInit(pFI);
  126.     }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. /*
  133.  * CPatronFrame::CreateCClient
  134.  *
  135.  * Purpose:
  136.  *  Constructs a new client specific to the application.
  137.  *
  138.  * Parameters:
  139.  *  None
  140.  *
  141.  * Return Value:
  142.  *  LPCClient       Pointer to the new client object.
  143.  */
  144.  
  145. LPCClient CPatronFrame::CreateCClient(void)
  146.     {
  147.     return (LPCClient)(new CPatronClient(m_hInst));
  148.     }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. /*
  156.  * CPatronFrame::FRegisterAllClasses
  157.  *
  158.  * Purpose:
  159.  *  Registers all classes used in this application.
  160.  *
  161.  * Parameters:
  162.  *  None
  163.  *
  164.  * Return Value:
  165.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  166.  */
  167.  
  168. BOOL CPatronFrame::FRegisterAllClasses(void)
  169.     {
  170.     WNDCLASS        wc;
  171.  
  172.     //First let the standard frame do its thing
  173.     if (!CFrame::FRegisterAllClasses())
  174.         return FALSE;
  175.  
  176.     //We need double-clicks now and for object activation later.
  177.     wc.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  178.     wc.hInstance     = m_hInst;
  179.     wc.cbClsExtra    = 0;
  180.     wc.lpfnWndProc   = PagesWndProc;
  181.     wc.cbWndExtra    = CBPAGESWNDEXTRA;
  182.     wc.hIcon         = NULL;
  183.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  184.     wc.hbrBackground = (HBRUSH)(COLOR_APPWORKSPACE+1);
  185.     wc.lpszMenuName  = NULL;
  186.     wc.lpszClassName = SZCLASSPAGES;
  187.  
  188.     if (!RegisterClass(&wc))
  189.         return FALSE;
  190.  
  191.     return TRUE;
  192.     }
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*
  199.  * CPatronFrame::OnCommand
  200.  *
  201.  * Purpose:
  202.  *  WM_COMMAND handler for the Patron frame window that processes extra
  203.  *  File menu items as well as the Page menu.
  204.  *
  205.  * Parameters:
  206.  *  hWnd            HWND of the frame window.
  207.  *  wParam          WPARAM of the message.
  208.  *  lParam          LPARAM of the message.
  209.  *
  210.  * Return Value:
  211.  *  LRESULT         Return value for the message.
  212.  */
  213.  
  214. LRESULT CPatronFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  215.     {
  216.     LPCPatronDoc    pDoc;
  217.  
  218.     COMMANDPARAMS(wID, wCode, hWndMsg);
  219.  
  220.     /*
  221.      * Don't bother with anything during first initialization,
  222.      * skipping many GizmoBar notifications.
  223.      */
  224.     if (m_fInit)
  225.         return 0L;
  226.  
  227.     pDoc=(LPCPatronDoc)m_pCL->ActiveDocument();
  228.  
  229.     switch (wID)
  230.         {
  231.         case IDM_FILEPRINT:
  232.             pDoc->Print(m_hWnd);
  233.             return 0L;
  234.  
  235.         case IDM_FILEPRINTERSETUP:
  236.             pDoc->PrinterSetup(m_hWnd, FALSE);
  237.             return 0L;
  238.  
  239.  
  240.         case IDM_EDITPASTESPECIAL:
  241.             pDoc->FPasteSpecial(m_hWnd);
  242.             return 0L;
  243.  
  244.         case IDM_EDITDELETEOBJECT:
  245.             pDoc->Delete();
  246.             return 0L;
  247.  
  248.         case IDM_PAGENEWPAGE:
  249.             pDoc->NewPage();
  250.             break;
  251.  
  252.         case IDM_PAGEDELETEPAGE:
  253.             pDoc->DeletePage();
  254.             break;
  255.  
  256.         case IDM_PAGENEXTPAGE:
  257.             pDoc->NextPage();
  258.             break;
  259.  
  260.         case IDM_PAGEPREVIOUSPAGE:
  261.             pDoc->PreviousPage();
  262.             break;
  263.  
  264.         case IDM_PAGEFIRSTPAGE:
  265.             pDoc->FirstPage();
  266.             break;
  267.  
  268.         case IDM_PAGELASTPAGE:
  269.             pDoc->LastPage();
  270.             break;
  271.  
  272.  
  273.         default:
  274.            return CFrame::OnCommand(hWnd, wParam, lParam);
  275.         }
  276.  
  277.     return 0L;
  278.     }
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287. /*
  288.  * CPatronFrame::CreateGizmos
  289.  *
  290.  * Purpose:
  291.  *  Procedure to create all the necessary gizmobar buttons.
  292.  *
  293.  * Parameters:
  294.  *  None
  295.  *
  296.  * Return Value:
  297.  *  UINT            Number of gizmos added to the bar.
  298.  */
  299.  
  300. UINT CPatronFrame::CreateGizmos(void)
  301.     {
  302.     UINT            iLast;
  303.     UINT            uState=GIZMO_NORMAL;
  304.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  305.  
  306.     //Insert the standard ones.
  307.     iLast=CFrame::CreateGizmos();
  308.  
  309.     //Insert Print File Import in the 5th position and account for it in iLast.
  310.     m_pGB->Add(utCmd, 4, IDM_FILEPRINT, m_dxB, m_dyB, NULL, NULL, 6, uState);
  311.     iLast++;
  312.  
  313.     //Add New Page, and Delete Page
  314.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEWPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  315.     m_pGB->Add(utCmd, iLast++, IDM_PAGEDELETEPAGE, m_dxB, m_dyB, NULL, m_hBmp, 3, uState);
  316.  
  317.     //Separator
  318.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  319.  
  320.     //First, Prev, Next, Last pages.
  321.     m_pGB->Add(utCmd, iLast++, IDM_PAGEFIRSTPAGE,    m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  322.     m_pGB->Add(utCmd, iLast++, IDM_PAGEPREVIOUSPAGE, m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  323.     m_pGB->Add(utCmd, iLast++, IDM_PAGENEXTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  324.     m_pGB->Add(utCmd, iLast++, IDM_PAGELASTPAGE,     m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  325.  
  326.     return iLast;
  327.     }
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. /*
  336.  * CPatronFrame::UpdateMenus
  337.  *
  338.  * Purpose:
  339.  *  Handles the WM_INITMENU message for the frame window.  Depending
  340.  *  on the existence of an active window, menu items are selectively
  341.  *  enabled and disabled.
  342.  *
  343.  * Parameters:
  344.  *  hMenu           HMENU of the menu to intialize
  345.  *  iMenu           UINT position of the menu.
  346.  *
  347.  * Return Value:
  348.  *  None
  349.  */
  350.  
  351. void CPatronFrame::UpdateMenus(HMENU hMenu, UINT iMenu)
  352.     {
  353.     LPCPatronDoc    pDoc;
  354.     BOOL            fOK=FALSE;
  355.     BOOL            fCallDefault=TRUE;
  356.     UINT            uTemp;
  357.     UINT            uTempE;
  358.     UINT            uTempD;
  359.  
  360.     pDoc=(LPCPatronDoc)m_pCL->ActiveDocument();
  361.  
  362.     uTempE=MF_ENABLED | MF_BYCOMMAND;
  363.     uTempD=MF_DISABLED | MF_GRAYED | MF_BYCOMMAND;
  364.     uTemp=((NULL!=pDoc) ? uTempE : uTempD);
  365.  
  366.  
  367.     //File menu:  If there is no current document win